package com.example.administrator.social.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.social.R;
import com.example.administrator.social.model.Contact;
import com.example.administrator.social.util.HttpUtils;

import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

/**
 * Created by Administrator on 2018/9/12 0012.
 */
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder>{
    private static final String INDEX_STRING_TOP = "↑";
    private static final int TOP_LAYPUT = 1;
    private List<Contact> mDataList;
    private LayoutInflater mInflater;
    private Context mContext;
    private ContactCallback mCallback;
    private int mUnReadCount = 0;
    public ContactAdapter(Context context, List<Contact> data) {
        mContext = context;
        mDataList = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.contact_recycler_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,  int position) {
         final int pt = position;
        Contact contact=null;
        if(position==0){
             contact = new Contact("新的朋友", HttpUtils.HTTP_URL+"img/friend.png",true,INDEX_STRING_TOP);
        }else{
             contact = mDataList.get(position-1);
        }

        if(contact.isTop()){
            QBadgeView mUnreadCountBadgeView = new QBadgeView(mContext);
            Badge mBadge = mUnreadCountBadgeView.bindTarget(holder.unreadCount);
            if(mUnReadCount>0){
                mBadge.setBadgeNumber(mUnReadCount);
                mBadge.setBadgeTextSize(12.0f,true);
                holder.unreadCount.setAlpha(0.0f);
            }
            holder.unreadCount.setVisibility(mUnReadCount>0?View.VISIBLE:View.GONE);
            if (mUnReadCount>0){
                mUnreadCountBadgeView.setVisibility(View.VISIBLE);
            }else{
                mUnreadCountBadgeView.hide(false);
                mUnreadCountBadgeView.setVisibility(View.GONE);
                mBadge.hide(false);
            }
        }


         String showName  = contact.getNickName();
         if(showName==null||showName.trim().equals("")){
             holder.noteText.setText(contact.getName());
         }else{
             holder.noteText.setText(showName);
         }
         //holder.headerImage.setImageResource(contact.getHeaderImage());
        if (contact.getHeaderImage()==null||contact.getHeaderImage().equals("")){
            Glide.with(mContext).load(HttpUtils.HTTP_URL+"img/defualt_header_icon.jpeg").into(holder.headerImage);
        }else{
            Glide.with(mContext).load(contact.getHeaderImage()).into(holder.headerImage);
        }
        holder.itemView.setOnClickListener(new ItemListener(contact,holder.itemView,position));

    }



    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mDataList.size()+1;
    }

    public void setDatas(List<Contact> datas) {
        this.mDataList = datas;
    }

    public void setUnReadCount(int unReadCount) {
        mUnReadCount = unReadCount;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView headerImage;
        private TextView noteText;
        private TextView unreadCount;
        public ViewHolder(View itemView) {
            super(itemView);
            noteText = itemView.findViewById(R.id.contact_recycler_item_header_text);
            headerImage = itemView.findViewById(R.id.contact_recycler_item_header_image);
            unreadCount = itemView.findViewById(R.id.unread_contact_request_count);
        }
    }

    public void setCallback(ContactCallback mCallback) {
        this.mCallback = mCallback;
    }

    public interface ContactCallback{
         void itemOnClick(View view, int position);

         //the top item e: new friends or char group
        void topItemOnClick(View view, int position);
    }

    private class  ItemListener implements View.OnClickListener{
        Contact ct = null;
        View view = null;
        int ps = 0;
        public  ItemListener(Contact contact,View v,int position){
            ct = contact;
            view=v;
            ps = position;
        }

        @Override
        public void onClick(View v) {
            if(ct.isTop()){
                mCallback.topItemOnClick(view,ps);
            }else{
                mCallback.itemOnClick(view,ps-TOP_LAYPUT);
            }
        }
    }

}
